<?php

namespace Common\Service;

use Common\Service\SignatureHelper;
use Think\Exception;
use Think\Log;

/**
 * 发送短信
 */
class ChuanglanSms
{
    //参数的配置 请登录zz.253.com 获取以下API信息 ↓↓↓↓↓↓↓
    const API_SEND_URL = 'http://smssh1.253.com/msg/send/json'; //创蓝发送短信接口URL
    const API_VARIABLE_URL = 'http://smssh1.253.com/msg/variable/json';//创蓝变量短信接口URL
    const API_BALANCE_QUERY_URL = 'http://smssh1.253.com/msg/balance/json';//创蓝短信余额查询接口URL
    const API_ACCOUNT = 'N0572332'; // 创蓝API账号
    const API_PASSWORD = 'LsgoWqwCKee324';// 创蓝API密码

    /**
     * 发送短信
     * @param $mobile
     * @param $code
     * @param string $needstatus
     * @return mixed
     */
    public static function sendSMS($mobile, $code, $needstatus = 'true')
    {
        $msg = '【盘古之手】提醒您验证码是：' . $code . '，请在10分钟内输入。请勿告诉其他人。';
        //创蓝接口参数
        $postArr = array(
            'account' => self::API_ACCOUNT,
            'password' => self::API_PASSWORD,
            'msg' => urlencode($msg),
            'phone' => $mobile,
            'report' => $needstatus
        );
        $result = self::curlPost(self::API_SEND_URL, $postArr);
        return $result;
    }

    /**
     * 发送变量短信
     * @param $msg
     * @param $params
     * @return mixed
     */
    public static function sendVariableSMS($msg, $params)
    {
        global $chuanglan_config;
        //创蓝接口参数
        $postArr = array(
            'account' => self::API_ACCOUNT,
            'password' => self::API_PASSWORD,
            'msg' => $msg,
            'params' => $params,
            'report' => 'true'
        );

        $result = self::curlPost(self::API_VARIABLE_URL, $postArr);
        return $result;
    }

    /**
     * 查询额度
     */
    public static function queryBalance()
    {
        global $chuanglan_config;
        //查询参数
        $postArr = array(
            'account' => self::API_ACCOUNT,
            'password' => self::API_PASSWORD,
        );
        $result = self::curlPost(self::API_BALANCE_QUERY_URL, $postArr);
        return $result;
    }

    /**
     * 通过CURL发送HTTP请求
     * @param string $url //请求URL
     * @param array $postFields //请求参数
     * @return mixed
     *
     */
    private static function curlPost($url, $postFields)
    {
        $postFields = json_encode($postFields);

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                'Content-Type: application/json; charset=utf-8'   //json版本需要填写  Content-Type: application/json;
            )
        );
        curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
        curl_setopt($ch, CURLOPT_TIMEOUT, 60);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        $ret = curl_exec($ch);
        if (false == $ret) {
            $result = curl_error($ch);
        } else {
            $rsp = curl_getinfo($ch, CURLINFO_HTTP_CODE);
            if (200 != $rsp) {
                $result = "请求状态 " . $rsp . " " . curl_error($ch);
            } else {
                $result = $ret;
            }
        }
        curl_close($ch);
        return $result;
    }
}